import os
import sys
import logging
import pygame

try:
    __file__
except NameError as e:
    from whereis import mymain
    rootdir = mymain()
else:
    rootdir = os.path.dirname(os.path.dirname(__file__))

sys.path.insert(0, rootdir)
from lib.bashlog import getlogger
from lib.tracemore import get_exc_plus
from lib.minsix import callable
from lib.config import Config
from lib.rec import Button
from lib.sprite import Sprite
from lib.sprite import Group
from lib import color
from lib import tool
from lib import events
sys.path.pop(0)

logger = logging.getLogger('wordson.mainpage')
cfg = Config()

class MainPage(Group):
    _title_font = None
    this_page = cfg.page_main

    def __init__(self):
        super(MainPage, self).__init__()

        screenx = cfg.screen[0] // 2

        # title
        big_font = cfg.new_font(cfg.titlesize)
        title = Sprite()
        title.image = tool.render(big_font, cfg.title)
        title.rect = title.image.get_rect()
        title.rect.top = cfg.title_offset
        title.rect.centerx = screenx
        self.add(title)

        # button
        # start btn
        btn_font = cfg.new_font(cfg.btn_fontsize)
        img = tool.render(btn_font, cfg.start)
        rect = img.get_rect()
        rect.width += cfg.btn_h_offset * 2
        rect.height += cfg.btn_v_offset * 2
        rect.centerx = screenx
        rect.top = title.rect.bottom + cfg.start_btn_gap
        startbtn = Button(img, rect, events.SWITCHPAGE, to=cfg.page_lang, by=self.this_page)
        self.add(startbtn)

        # load save botton
        img = tool.render(btn_font, cfg.load)
        rect = img.get_rect()
        rect.width += cfg.btn_h_offset * 2
        rect.height += cfg.btn_v_offset * 2
        rect.centerx = screenx
        rect.top = startbtn.rect.bottom + cfg.btn_gap
        loadbtn = Button(img, rect, events.SWITCHPAGE, to=cfg.page_load, by=self.this_page)
        self.add(loadbtn)

        # exit button
        img = btn_font.render(cfg.exit, True, color.black, None)
        rect = img.get_rect()
        rect.height += cfg.btn_v_offset * 2
        rect.width += cfg.btn_h_offset * 2
        rect.centerx = screenx
        rect.top = loadbtn.rect.bottom + cfg.btn_gap
        exitbtn = Button(img, rect, evtid=pygame.QUIT)
        self.add(exitbtn)


if __name__ == '__main__':
    getlogger(logger, logging.DEBUG)

    pygame.init()
    pygame.key.set_repeat(cfg.key_repeat_delay, cfg.key_repeat_interval)
    pygame.display.set_caption(cfg.title)

    screen = pygame.display.set_mode(cfg.screen)

    run = True
    clock = pygame.time.Clock()
    page = MainPage()
    # btn = Button("test")

    while run:
        for event in pygame.event.get():
            if page.handle(event):
                continue
            if event.type == pygame.QUIT:
                run = False

        inter = clock.tick(cfg.tick)
        page.update(inter)
        screen.fill(color.white)
        page.draw(screen)
        pygame.display.flip()

    pygame.quit()
